﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WoChu.RfClient.Entity;
using WoChu.RfClient.Common;
using WoChu.CF.BaseFrame.Util;

namespace WoChu.RfClient.Duty
{
    public partial class FrmPickLocationReplenish : Form
    {
        public FrmPickLocationReplenish()
        {
            InitializeComponent();
        }
        private static List<PickReplenishInfo> _lstPickReplenish = null;
        public static List<PickReplenishInfo> lstPickReplenish
        {
            get
            {
                if (_lstPickReplenish == null) _lstPickReplenish = new List<PickReplenishInfo>();
                return _lstPickReplenish;
            }
            set { _lstPickReplenish = value; }
        }

        private void txtBatchNo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;
            //if (string.IsNullOrEmpty(cbbCellNo.Text))
            //{
            //    MessageBox.Show("请先选择库位！");
            //    return;
            //}
            lstPickReplenish = Duty.PickReplenishUIRule.NewInstance.GetPickLocationGoods(txtBatchNo.Text.Trim(), Convert.ToDateTime(dtpRationDate.Text));
            if (lstPickReplenish == null || lstPickReplenish.Count <= 0)
            {
                MessageBox.Show("没有补货数据！");
                return;
            }
            this.bsPickReplenishSource.DataSource = null;
            this.bsPickReplenishSource.DataSource = lstPickReplenish;
            this.dtGridPickReplenish.DataSource = this.bsPickReplenishSource;
            this.dtGridPickReplenish.Refresh();
            this.txtBatchNo.SelectAll();

          
        }

        //private void cbbCellNo_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    if (string.IsNullOrEmpty(cbbCellNo.Text)) return;
        //    txtBatchNo.Focus();
        //    txtBatchNo.SelectAll();
        //}

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (lstPickReplenish == null || lstPickReplenish.Count() <= 0)
            {
                MessageBox.Show("没有可提交的数据！");
                return;
            }
            //List<int> lstPickID = new List<int>();
            //foreach (var item in lstPickReplenish)
            //{
            //    lstPickID.Add(item.ID);
            //}
            OutPutParams<bool> re = Duty.PickReplenishUIRule.NewInstance.UpdateReplenishStatus(LoginBean.Instance.USERCODE, lstPickReplenish);

            if (re.Params)
            {
                lstPickReplenish.Clear();
                this.bsPickReplenishSource.DataSource = null;
                this.bsPickReplenishSource.DataSource = lstPickReplenish;
                this.dtGridPickReplenish.DataSource = this.bsPickReplenishSource;
                this.dtGridPickReplenish.Refresh();
                this.txtBatchNo.Text = string.Empty;
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show("提交成功！");

            }
            else
            {
                MessageBox.Show("提交失败！\n" + re.Message);
                return;
            }

        }

        private void FrmPickLocationReplenish_Load(object sender, EventArgs e)
        {


            dtpRationDate.CustomFormat = "yyyy-MM-dd";
            dtpRationDate.Focus();
            //List<LocationGoodsInfo> lstPickCellNo = Duty.PickReplenishUIRule.NewInstance.GetPickCellNo();
            //if (lstPickCellNo == null) return;
            //this.cbbCellNo.DataSource = lstPickCellNo;
            //this.cbbCellNo.DisplayMember = "CELLNO";
            //this.cbbCellNo.ValueMember = "CELLNO";
        }

        private void dtGridPickReplenish_Click(object sender, EventArgs e)
        {
            int curIndex = dtGridPickReplenish.CurrentRowIndex;
            if(curIndex>=0)
                this.dtGridPickReplenish.Select(curIndex);
        }

        private void dtGridPickReplenish_DoubleClick(object sender, EventArgs e)
        {
            int curIndex = dtGridPickReplenish.CurrentRowIndex;
            if (curIndex < 0) return;
            try
            {
                string GoodsName =dtGridPickReplenish[curIndex, 2].ToString();

                if (MessageBoxEx.Question("商品：" + GoodsName + " 是否删除该数据？") == DialogResult.Yes)
                {
                    lstPickReplenish.RemoveAt(curIndex);
                    this.bsPickReplenishSource.DataSource = null;
                    this.bsPickReplenishSource.DataSource = lstPickReplenish;
                    this.dtGridPickReplenish.DataSource = this.bsPickReplenishSource;
                    this.dtGridPickReplenish.Refresh();
                }
            }
            catch (Exception ex)
            {
                TraceEx.Write(ex.Message);
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show("删除数据失败!");
            }
        }

        private void dtpRationDate_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;
            //cbbCellNo.Focus();
            txtBatchNo.Focus();
        }
    }
}